package com.wego.controller.admin;

import com.wego.bean.PageBean;
import com.wego.bean.ResultBean;
import com.wego.converter.CommentConverter;
import com.wego.domain.dto.CommentDTO;
import com.wego.domain.entity.Comment;
import com.wego.domain.query.CommentQuery;
import com.wego.domain.vo.CommentVO;
import com.wego.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论(tb_comment)表控制层
 *
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private CommentConverter commentConverter;

    @GetMapping("/getCommentWithSub")
    public ResultBean<PageBean<CommentVO>> getCommentWithSub(CommentQuery commentQuery) {
        final PageBean<CommentVO> commentWithSub = commentService.getCommentWithSubPage(commentQuery);
        return ResultBean.success(commentWithSub);
    }
    
    @GetMapping("/page")
    public ResultBean<PageBean<CommentVO>> page(CommentQuery commentQuery) {
        final PageBean<CommentVO> pageBean = commentService.getPage(commentQuery);
        return ResultBean.success(pageBean);
    }

    @GetMapping("/{id}")
    public ResultBean<CommentVO> getDetails(@PathVariable("id") Integer id) {
        final CommentVO comment = commentService.getById(id);
        return ResultBean.success(comment);
    }

    @PutMapping
    public ResultBean<Void> update(@RequestBody CommentDTO commentDTO) {
        final Comment comment = commentConverter.dto2entity(commentDTO);
        commentService.update(comment);
        return ResultBean.success("修改成功!", null);
    }

    @PostMapping
    public ResultBean<Void> insert(@RequestBody CommentDTO commentDTO) {
        final Comment comment = commentConverter.dto2entity(commentDTO);
        commentService.insert(comment);
        return ResultBean.success("添加成功!", null);
    }
    @DeleteMapping
    public ResultBean<Void> batchDelete(@RequestParam List<Integer> ids) {
        commentService.deleteByIds(ids);
        return ResultBean.success("批量删除成功!", null);
    }


}